#include "exparrival.h"
#include <QDebug>
#include <QTime>

// refer to http://en.wikipedia.org/wiki/Exponential_distribution

bool ExpArrival::seeded = false;

qreal ExpArrival::newArrival( qreal rate, qreal min, qreal max )
{
    if( !seeded )
    {
        qsrand( QTime::currentTime().msec() );
        seeded = true;
    }

    qreal prob = (qreal)( qrand() % 100 ) / 100;
    qreal res = - qLn( 1 - prob ) / rate;

    if( res < min || ( max > 0 && res > max ) )
    {
        return newArrival( rate, min, max );
    }

    return res;
}
